home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / GRAF3D / BOXES.C next >
C/C++ Source or Header  |  1992-07-19  |  5KB  |  229 lines

  1. /*╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤ÑÑÑ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤*/
  2. /*                                                                               */
  3. /* Boxes.c    --      Translation of Pascal 'LISA/EXAMPLE/BOXES.TEXT'            */
  4. /*                    from Apple Programmer's and Developer's Association        */
  5. /*                    disk 'Macintosh Example Applications and Sources v.1.0'    */
  6. /*                                                                               */
  7. /*                    Translation to LightSpeed C by Lewis E. Garrett - 9/27/90  */
  8. /*                                                   CIS 71147,2202               */
  9. /*                                                                               */
  10. /*                                                                               */
  11. /*╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤ÑÑÑ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤*/
  12.  
  13. #include <Graf3D.h>
  14.  
  15. /* CONST */
  16. #define boxCount 15
  17. #define keyOrMouse mDownMask+keyDownMask
  18. #define NIL 0L
  19.  
  20. #ifndef abs
  21. #define abs(x)        (((x)<0)?(-(x)):(x))
  22. #endif
  23.  
  24. typedef struct Box3D{
  25.     Point3D pt1;
  26.     Point3D pt2;
  27. };
  28.  
  29. WindowPtr w;
  30.  
  31. /*    GrafPort myPort;    */
  32. Port3D        gPort3D;
  33. struct        Box3D boxArray[15];
  34. int         nBoxes;
  35. int         i;
  36. EventRecord dummy;
  37.  
  38. Port3D        *ptr3D;
  39.  
  40. main()
  41.  
  42. { /* main program */
  43.     FlushEvents(everyEvent, 0);
  44.     InitGraf(&thePort);
  45.     ptr3D    = &gPort3D;
  46.     InitGrf3d(&ptr3D);
  47.     InitFonts();
  48.     InitWindows();
  49.     InitMenus();
  50.     TEInit();
  51.     InitDialogs(NIL);
  52.  
  53.     InitCursor();
  54.     HideCursor();
  55.  
  56.     w = NewWindow(NIL, &screenBits.bounds, "\p", TRUE, 2, (WindowPtr)(-1L), FALSE, 0L);
  57.     SetPort(w);
  58.  
  59.  
  60.     Draw3D();
  61.  
  62.     DisposeWindow(w);
  63.  
  64. }
  65.  
  66. Draw3D()
  67. {
  68.     Rect inRect;
  69.     long n;
  70.  
  71.     Open3DPort(&gPort3D);
  72.     ViewPort(&thePort->portRect); /* put the image in this rect */
  73.     LookAt(FixRatio(-100, 1), FixRatio(75, 1), FixRatio(100, 1),
  74.     FixRatio(-75, 1)); /* aim the camera into 3D space */
  75.     ViewAngle(FixRatio(30, 1)); /* choose lens focal length */
  76.     Identity();
  77.     Roll(FixRatio(20, 1));
  78.     Pitch(FixRatio(70, 1)); /* roll and pitch the plane */
  79.  
  80.     while (! OSEventAvail(keyOrMouse, &dummy))
  81.     {
  82.         nBoxes = 0;
  83.  
  84.         do
  85.         {
  86.             MakeBox();
  87.         }
  88.         while (nBoxes<boxCount);
  89.  
  90.         PenPat(white);
  91.         BackPat(black);
  92.         EraseRect(&thePort->portRect);
  93.  
  94.         for ( i = -10; i<=10; i++ )
  95.         {
  96.             MoveTo3D(FixRatio(i*10, 1), FixRatio(-100, 1), 0);
  97.             LineTo3D(FixRatio(i*10, 1), FixRatio(100, 1), 0);
  98.         }
  99.  
  100.         for ( i = -10; i<=10; i++ )
  101.         {
  102.             MoveTo3D(FixRatio(-100, 1), FixRatio(i*10, 1), 0);
  103.             LineTo3D(FixRatio(100, 1), FixRatio(i*10, 1), 0);
  104.         }
  105.  
  106.         for ( i = nBoxes-1; i>=0; i-- )
  107.         {
  108.             DrawBrick(boxArray[i].pt1,boxArray[i].pt2);
  109.         }
  110.  
  111.         for ( n = 1; n<=1000000; n++ )
  112.         {
  113.             n = n;
  114.         }
  115.  
  116.     }
  117.  
  118.     ShowCursor();
  119.  
  120. }
  121.  
  122. DrawBrick(pt1,pt2)
  123. Point3D pt1,pt2;
  124.  
  125. {
  126.     RgnHandle tempRgn;
  127.  
  128. /*    BackColor(Random());        enable for randomly colored boxes */
  129.     tempRgn = NewRgn();
  130.     OpenRgn();
  131.     MoveTo3D(pt1.x, pt1.y, pt1.z); /* front face, y=y1 */
  132.     LineTo3D(pt1.x, pt1.y, pt2.z);
  133.     LineTo3D(pt2.x, pt1.y, pt2.z);
  134.     LineTo3D(pt2.x, pt1.y, pt1.z);
  135.     LineTo3D(pt1.x, pt1.y, pt1.z);
  136.     CloseRgn(tempRgn);
  137.     FillRgn(tempRgn, white);
  138.  
  139.     OpenRgn();
  140.     MoveTo3D(pt1.x, pt1.y, pt2.z); /* top face, z=z2 */
  141.     LineTo3D(pt1.x, pt2.y, pt2.z);
  142.     LineTo3D(pt2.x, pt2.y, pt2.z);
  143.     LineTo3D(pt2.x, pt1.y, pt2.z);
  144.     LineTo3D(pt1.x, pt1.y, pt2.z);
  145.     CloseRgn(tempRgn);
  146.     FillRgn(tempRgn, gray);
  147.  
  148.     OpenRgn();
  149.     MoveTo3D(pt2.x, pt1.y, pt1.z); /* right face, x=x2 */
  150.     LineTo3D(pt2.x, pt1.y, pt2.z);
  151.     LineTo3D(pt2.x, pt2.y, pt2.z);
  152.     LineTo3D(pt2.x, pt2.y, pt1.z);
  153.     LineTo3D(pt2.x, pt1.y, pt1.z);
  154.     CloseRgn(tempRgn);
  155.     FillRgn(tempRgn, black);
  156.  
  157.     PenPat(white);
  158.     MoveTo3D(pt2.x, pt2.y, pt2.z); /* outline right */
  159.     LineTo3D(pt2.x, pt2.y, pt1.z);
  160.     LineTo3D(pt2.x, pt1.y, pt1.z);
  161.     PenNormal();
  162.  
  163.     DisposeRgn(tempRgn);
  164. }
  165.  
  166. MakeBox()
  167.  
  168. {
  169.     struct Box3D myBox;
  170.     int i, j, h, v;
  171.     Point3D p1, p2;
  172.     Rect myRect;
  173.     Rect testRect;
  174.     short sw;
  175.  
  176.     p1.x = FixRatio((Random() % 70 - 15), 1);
  177.     p1.y = FixRatio((Random() % 70 - 10), 1);
  178.  
  179.     p1.z = 0;
  180.  
  181.     p2.x = p1.x + FixRatio((10 + abs(Random()) % 30), 1);
  182.     p2.y = p1.y + FixRatio((10 + abs(Random()) % 45), 1);
  183.  
  184.     p2.z = p1.z + FixRatio((10 + abs(Random()) % 35), 1);
  185.  
  186.     /* reject box if it intersects one already in list */
  187.     SetRect(&myRect, HiWord(p1.x), HiWord(p1.y), HiWord(p2.x), HiWord(p2.y));
  188.  
  189.     i=0;
  190.     do
  191.     {
  192.         sw = 0;             /* clear exit switch */
  193.         SetRect(&testRect, HiWord(boxArray[i].pt1.x), HiWord(boxArray[i].pt1.y),
  194.         HiWord(boxArray[i].pt2.x), HiWord(boxArray[i].pt2.y));
  195.         InsetRect(&testRect, -1, -1);
  196.         if ( SectRect(&myRect, &testRect, &testRect) )
  197.             break; /* EXIT(MakeBox); */
  198.         sw = 1;
  199.         i=i+1;
  200.     }
  201.     while (i<(nBoxes-1));
  202.  
  203.     if (sw==1)                /* sw=1 means previous loop finished */
  204.     {
  205.         myBox.pt1 = p1;
  206.         myBox.pt2 = p2;
  207.  
  208.         /* calc midpoint of box and its distance from the eye */
  209.  
  210.         i = 0;
  211.         boxArray[nBoxes].pt1 = myBox.pt1; /* sentinel */
  212.         boxArray[nBoxes].pt2 = myBox.pt2;
  213.  
  214.         while (
  215.         ((myBox.pt1.y > boxArray[i].pt2.y) && (myBox.pt2.y > boxArray[i].pt1.y)) ||
  216.             ((myBox.pt1.x < boxArray[i].pt2.x) && (myBox.pt2.x < boxArray[i].pt1.x)) )
  217.             i = i + 1;        /* insert in order of dist */
  218.  
  219.         for ( j = nBoxes; j>=(i+1); j-- )
  220.         {
  221.             boxArray[j] = boxArray[j - 1];
  222.         }
  223.         boxArray[i] = myBox;
  224.         nBoxes = nBoxes + 1;
  225.     }
  226. }
  227.  
  228.  
  229.